$(function () {
    get_category()
    function get_category() {
        $.ajax({
            type: 'get',
            url: '/admin/category/list',
            data: {},
            success: function (backData) {
                let optionHtml = template('art_category', backData)
                $('#categoryId').html(optionHtml)

                get_article_data()
            }
        })

        function get_article_data() {
            // 1.0 获取url传入的id参数值
            let id = window.location.search.split('=')[1]
            $.ajax({
                type: 'get',
                url: '/admin/article/search',
                data: {
                    id
                },
                success: function (backData) {
                    console.log(backData);
                    let data = backData.data;
                    $('#inputTitle').val(data.title);
                    $('.article_cover').attr('src', data.cover)
                    $('#category').val(data.categoryId);
                    $('#articleDate').val(data.date);
                    $('#articleContent').val(data.content);
                }
            })  
        }

        $('.btn-edit').on('click',function(e){
            e.preventDefault()
            let id = window.location.search.split('=')[1]
       
            let fd = new FormData($('form')[0])
            let aaaa= $('#categoryId').val()
            let  bbbb=$(`#articleContent`).val()
            fd.categoryId=aaaa
            fd.append('id',id)
            // fd.append('id',id)
            // console.log(fd.content,fd.id,fd.categoryId)
            fd.append(`content`,bbbb)
            $.ajax({
                type:'post',
                url:'/admin/article/edit',
                data:fd,
                contentType:false,
                processData:false,
                success:function(backData){
                    console.log(backData.msg);
                    window.location.href = './article_list.html'
                }
            })
        })
    }










    // let id = window.location.search.split('=')[1]
    // $.ajax({
    //     type: 'get',
    //     url: '/admin/article/search',
    //     data: {
    //         id
    //     },
    //     success: function (backData) {
    //         console.log(backData);
    //         $('#inputTitle').val(backData.data.title)
    //         $('.article_cover').attr('src', backData.data.cover)
    //         $('#category').val(backData.data.categoryId);
    //         $('#articleDate').val(backData.data.date);
    //         $('#articleContent').val(backData.data.content);
    //     }
    // })
})